import java.sql.*;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class Main {
    public static void main(String[] args) {
        // 1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2.创建连接
            String url = "jdbc:mysql://127.0.0.1:3306/jdbc?characterEncoding=utf-8";
            Connection connection = DriverManager.getConnection(url, "root", "root");
            //if (connection != null) {
               // System.out.println("连接成功");
            //
            // 3、写sql
            String sql = "select * from user1";

            // 4、预编译sql
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            // 5、有参数设置参数

            // 6、执行sql
            ResultSet resultSet = preparedStatement.executeQuery();

            // 7、遍历结果集
            while (resultSet.next()){
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String password = resultSet.getString(3);
                System.out.println(id+"-----"+name+"------"+password);
            }

            // 8、关闭资源
            resultSet.close();
            preparedStatement.close();
            connection.close();






        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }
}